﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Windows.Forms;

namespace TeenyGrab.UserControls
{
    /// <summary>
    /// Interaction logic for ScreenControl.xaml
    /// </summary>
    public partial class ScreenControl : System.Windows.Controls.UserControl
    {
        private MouseHookListener MouseHook { get; set; }
        public bool NoScreenMeansAllScreens { get; set; }

        private Screen screen;
        public Screen Screen
        {
            get
            {
                return screen;
            }
            set
            {
                screen = value;
                if (screen != null)
                {
                    textBlock.Text = "Device Name: " + screen.DeviceName + "\r\n" +
                                     "Boundaries: " + screen.Bounds.X + "x" + screen.Bounds.Y + " " + screen.Bounds.Width + "x" + screen.Bounds.Height;
                }
                else
                {
                    textBlock.Text = NoScreenMeansAllScreens ? "All screens selected." : "No screen selected.";
                }
            }
        }

        public ScreenControl()
        {
            InitializeComponent();

            MouseHook = new MouseHookListener(new GlobalHooker());
            MouseHook.MouseClick += new System.Windows.Forms.MouseEventHandler(MouseHook_MouseClick);
        }

        void MouseHook_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Screen = Screen.FromPoint(e.Location);
                MouseHook.Enabled = false;
            }
            else
            {
                Screen = null;
                MouseHook.Enabled = false;
            }
        }

        private void buttonPick_Click(object sender, RoutedEventArgs e)
        {
            MouseHook.Enabled = true;
            textBlock.Text = "Left click in a screen to select it.\r\nRight click anywhere to cancel.";
        }
    }
}
